/**
 * Created on [2019/5/3 18:02] by 李瑞益
 *
 * 项目名称: cache
 *
 * 本程序版权属于福建慧政通信息科技有限公司所有。
 * 任何组织和个人未经福建慧政通信息科技有限公司许可与授权,不得擅自传播、复制、更改该程序的内容。
 * 本程序受版权法和国际条约的保护。如未经授权而擅自复制或传播本程序(或其中任何部分),
 * 将受到严厉的刑事及民事制裁，并将在法律许可的范围内受到最大可能的起诉!
 *
 * 2019 福建慧政通信息科技有限公司
 */
package com.lry.service.impl;

import java.util.Date;

import com.lry.bean.Employee;
import com.lry.dao.EmployeeMapper;
import com.lry.service.EmployeeService;
import com.sun.org.apache.bcel.internal.generic.LASTORE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

/**
 * 
 *
 * @author 李瑞益
 */
@Service
@CacheConfig(cacheNames="test-cache")
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
//    @Cacheable(value="test-cache",key="#a0")
    //key为参数
//    @Cacheable(value="test-cache",key="#id0")
    //key为被调用方法名+参数列表
//    @Cacheable(value="test-cache",key="#root.methodName+'['+#root.args[0]+']'")
    @Cacheable(/*value="test-cache",*/key="#root.targetClass+'.'+#root.methodName+'['+#root.args[0]+']'")
    //自定义生成器
//    @Cacheable(value="test-cache",keyGenerator = "keyGenerator")
//    @Cacheable(value="test-cache",keyGenerator = "keyGenerator",condition = "#a0 > 1")
//    @Cacheable(value="test-cache",keyGenerator = "keyGenerator",condition = "#a0 > 1 and #id != 2")
//    @Cacheable(value="test-cache",keyGenerator = "keyGenerator",condition = "#a0 > 1 and #root.methodName eq 'getById'")
//    @Cacheable(value="test-cache",keyGenerator = "keyGenerator",unless = "#a0 == 1")
    public Employee getById(Integer id) {
        System.out.println("操作数据库查询！"+new Date());
        return employeeMapper.getById(id);
    }

    /**
     * 先调用方法，然后更新缓存数据
     * 该注解和cacheable类似
     * 1、先调用目标方法
     * 2、缓存目标方法
     * @param employee
     */
    @Override
    //如果通过参数id作为key存储的，下面两种方式
//    @CachePut(value="test-cache",key = "#employee.id")
//    @CachePut(value="test-cache",key = "#result.id")
    //
    @CachePut(value = "test-cache",key = "#root.targetClass+'.getById['+#employee.id+']'")
    public Employee update(Employee employee) {
        System.out.println(employee);
        employeeMapper.update(employee);
        return employee;
    }

    @Override
    @CacheEvict(value="test-cache",key="#root.targetClass+'.getById['+#root.args[0]+']'")
    public void delete(Integer id) {
        int i = employeeMapper.delete(id);
    }

    @Override
    //Cache Put
    @Caching(
            cacheable = {
                    @Cacheable(value="test-cache",key="#lastName")
            },
            put = {
                    @CachePut(value="test-cache",key = "#result.age"),
                    @CachePut(value="test-cache",key ="#root.targetClass+'.getById['+#result.id+']'")
            }
    )
    public Employee getEmployeeLastName(String lastName) {
        return employeeMapper.getEmployeeLastName(lastName);
    }
}
